package com.atguigu.szdz.dcsyl.controller;

import com.alibaba.excel.EasyExcel;
import com.atguigu.szdz.dcsyl.common.listener.QtListener;
import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.entity.FcDataEntity;
import com.atguigu.szdz.dcsyl.entity.QtDataEntity;
import com.atguigu.szdz.dcsyl.service.impl.YlDataServiceImpl;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qtData")
public class QtDataController {

    @Autowired
    private YlDataServiceImpl fcDataService;
    @Autowired
    private MapperUtils mapperUtils;

    /**
     * 获取其他数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/getQtData")
    public R getQtData(@RequestBody Map<String, Integer> map) {
        //获取井ID
        Integer wellId = map.get("wellId");
        //获取当前页
        Integer currentPage = map.get("currentPage");
        //获取每页显示条数
        Integer pageSize = map.get("pageSize");
        //获取总条数
        String sql = "select count(*) from dc_qtdata where well_id=" + wellId;
        List<Map<String, Object>> contList = mapperUtils.queryBySql(sql);
        Integer total = Integer.parseInt(contList.get(0).get("count").toString());
        //总页数
        Integer totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        //获取数据
        List<Map<String, Object>> qtListData = fcDataService.getQtData(wellId, currentPage, pageSize);
        return R.ok().put("qtListData", qtListData).put("total", total).put("totalPage", totalPage);
    }

    /**
     * 导入其他数据的excel文件
     *
     * @param file
     * @param wellId
     * @return
     */
    @Operation(summary = "解析其他数据的excel文件")
    @PostMapping("/importFile")
    public R importFile(@RequestParam("file") MultipartFile file, @RequestParam("well_id") Integer wellId) {
        //先去数据库查询是否有该井
        String sql = "select * from dc_well_info where id=" + wellId;
        List<Map<String, Object>> wellList = mapperUtils.queryBySql(sql);
        //获取井名
        String wellName = wellList.get(0).get("well_name").toString();
        List<QtDataEntity> list = new ArrayList<>();
        if (wellList.size() == 0) {
            return R.error("井不存在");
        }
        if (file.isEmpty()) {
            return R.error("文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
            return R.error("文件格式错误");
        }
        try (InputStream inputStream = file.getInputStream()) {
            QtListener qtListener = new QtListener();
            //读取excel文件
            EasyExcel.read(inputStream, QtDataEntity.class, qtListener).headRowNumber(3).sheet().doRead();
            //获取读到的数据
            list = qtListener.getList();
            return R.ok("解析成功").put("qtListData", list);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("文件导入失败");
        }
    }

    /**
     * 导入其他数据
     */
    @Operation(summary = "导入其他数据")
    @PostMapping("/importQtData")
    public R importQtData(@RequestBody Map<String, Object> map) {
        //获取井ID
        Integer wellId = Integer.parseInt(map.get("wellId").toString());
        //获取其他数据列表并且vo->po
        List<QtDataEntity> qtList = new ArrayList<>();
        //获取其他数据列表并且vo->po
        List<Map<String, Object>> qtListData = (List<Map<String, Object>>) map.get("qtListData");
        for (Map<String, Object> qtMap : qtListData) {
            QtDataEntity qtDataEntity = new QtDataEntity();
            //vo->po
            qtDataEntity.setBottomDepth(qtMap.get("bottomDepth") != null ? Double.parseDouble(qtMap.get("bottomDepth").toString()) : null);
            qtDataEntity.setTopDepth(qtMap.get("topDepth") != null ? Double.parseDouble(qtMap.get("topDepth").toString()) : null);
            qtDataEntity.setSampleType(qtMap.get("sampleType") != null ? qtMap.get("sampleType").toString() : null);
            qtDataEntity.setStrata(qtMap.get("strata") != null ? qtMap.get("strata").toString() : null);
            qtDataEntity.setLithId(qtMap.get("lithId") != null ? qtMap.get("lithId").toString() : null);
            qtDataEntity.setS0(qtMap.get("s0") != null ? Double.parseDouble(qtMap.get("s0").toString()) : null);
            qtDataEntity.setS1(qtMap.get("s1") != null ? Double.parseDouble(qtMap.get("s1").toString()) : null);
            qtDataEntity.setS2(qtMap.get("s2") != null ? Double.parseDouble(qtMap.get("s2").toString()) : null);
            qtDataEntity.setPg(qtMap.get("pg") != null ? Double.parseDouble(qtMap.get("pg").toString()) : null);
            qtDataEntity.setGpi(qtMap.get("gpi") != null ? Double.parseDouble(qtMap.get("gpi").toString()) : null);
            qtDataEntity.setOpi(qtMap.get("opi") != null ? Double.parseDouble(qtMap.get("opi").toString()) : null);
            qtDataEntity.setTpi(qtMap.get("tpi") != null ? Double.parseDouble(qtMap.get("tpi").toString()) : null);
            qtDataEntity.setOilProperty(qtMap.get("oilProperty") != null ? qtMap.get("oilProperty").toString() : null);
            qtDataEntity.setConclusion(qtMap.get("conclusion") != null ? qtMap.get("conclusion").toString() : null);
            qtDataEntity.setRemark(qtMap.get("remark") != null ? qtMap.get("remark").toString() : null);
            qtDataEntity.setTMax(qtMap.get("tmax") != null ? Double.parseDouble(qtMap.get("tmax").toString()) : null);
            qtList.add(qtDataEntity);
        }
        //删除其他数据
        fcDataService.removeQtData(wellId);
        //批量插入其他数据
        fcDataService.saveBatchQt(qtList, wellId);
        return R.ok("导入成功");
    }
}
